<style scoped>
    .company >>> .layui-form-select {
        z-index: auto;
    }


    #acts {
        font-size: 0;
    }
    #acts span {
        font-size: 14px;
        cursor: pointer;
    }
    /*.oitem {*/
    /*    padding: 20px;*/
    /*    background: #f2f2f2;*/
    /*    width: 640px;*/
    /*}*/
    .rj-oitem-tit {
        display: block;
        padding: 9px 20px;
        font-weight: 400;
        line-height: 20px;
        text-align: right;
    }
</style>

<template>
    <div class="company">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>修改合同</legend>
        </fieldset>
        <form class="layui-form " >
            <input type="hidden" name="setlwgtype" value="2" />
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="required" placeholder="请输入合同名称" :value="adata.name" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">采收主体</label>
                <div class="layui-input-block">
                    <select type="text" name="organizationid" lay-verify="required" lay-filter="organizationid" class="layui-select" lay-search>
                        <option value="">请选择</option>
                        <option v-for="(item, index) in companys" :value="item.guid">{{item.name}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">批次</label>
                <div class="layui-input-block" ref="batchmain">
                    <select type="text" name="batchguid" lay-verify="required" class="layui-select">
                        <option value="">请选择</option>
                        <option v-for="(item, index) in batches" :key="index" :value="item.guid" :selected="item.guid == adata.batchguid">{{item.name}}</option>
                    </select>
                </div>
            </div>
<!--            <div class="layui-form-item">-->
<!--                <label class="layui-form-label">部门</label>-->
<!--                <div class="layui-input-block">-->
<!--                    <input type="hidden" name="departname" :value="depart.name" />-->
<!--                    <select type="text" name="departid" lay-filter="departid" lay-verify="required" class="layui-select">-->
<!--                        <option value="" disabled>请选择</option>-->
<!--                        <option v-for="(item, index) in departments" :key="item.guid" :value="item.guid" :selected="item.guid == adata.departid">{{item.name}}</option>-->
<!--                    </select>-->
<!--                </div>-->
<!--            </div>-->
            <div class="layui-form-item">
                <label class="layui-form-label">乙方</label>
                <div class="layui-input-block">
                    <select type="text" name="partb" lay-verify="required" class="layui-select" lay-search>
                        <option value="" disabled>请选择</option>
                        <option v-for="(item, index) in plantors" :key="index" :value="item.guid" :selected="item.guid == adata.partb">{{item.realname}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">地块</label>
                <div class="layui-input-block">
                    <select type="text" name="arealandid" lay-verify="required" class="layui-select">
                        <option value="" disabled>请选择</option>
                        <option v-for="(item, index) in arealands" :key="index" :value="item.guid" :selected="item.guid == adata.arealandid">{{item.AName}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">入库库房</label>
                <div class="layui-input-block">
                    <!--                    <input type="hidden" name="srname" ref="srname" />-->
                    <select type="text" name="srguid" lay-verify="required" class="layui-select" lay-search>
                        <option value="" disabled>请选择</option>
                        <option v-for="(item, index) in rooms" :key="index" :value="item.guid" :selected="item.guid == adata.srguid">{{item.name}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">产品</label>
                <div class="layui-input-block">
                    <input type="hidden" name="productname" ref="productname" />
                    <select type="text" name="productguid" lay-verify="required" class="layui-select" lay-search>
                        <option value="" disabled>请选择</option>
                        <option v-for="(item, index) in products" :key="index" :value="item.guid" :selected="item.guid == adata.productguid">{{item.productname}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">规格</label>
                <div class="layui-input-block">
                    <select type="text" name="specificationid" lay-verify="required" class="layui-select" lay-search>
                        <option value="" disabled>请选择</option>
                        <option v-for="(item, index) in specs" :key="index" :value="item.guid" :selected="item.guid == adata.specificationid">{{item.specification}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">单价(元/吨)</label>
                <div class="layui-input-block">
                    <input type="number" name="price" ref="price" placeholder="请输入单价" :value="adata.price" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">质检标准</label>
                <div class="layui-input-block">
                    <select class="weui-select" lay-verify="required" name="qamainid">
                        <option value="">请选择</option>
                        <option v-for="qamain in qamains" :value="qamain.guid" :selected="item.guid == adata.qamainid">{{qamain.qaname}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">质检方式</label>
                <div class="layui-input-block">
                    <input type="radio" checked name="qatype" lay-filter="qatype" value="1" title="库房质检" :checked="adata.qatype == 1">
                    <input type="radio" name="qatype" lay-filter="qatype" value="2" title="地头质检" :checked="adata.qatype == 2">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">支付方式</label>
                <div class="layui-input-block">
                    <input type="radio" name="paymentmethod" lay-filter="paymentmethod" value="1" title="装车付" :checked="adata.paymentmethod == 1">
                    <input type="radio" checked name="paymentmethod" lay-filter="paymentmethod" value="2" title="验收付" :checked="adata.paymentmethod == 2">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">扣杂方式</label>
                <div class="layui-input-block">
                    <input type="radio" name="deducttype" value="1" title="统一扣杂" :checked="adata.deducttype == 1" lay-filter="deducttype">
                    <input type="radio" name="deducttype" value="2" title="分步扣杂" :checked="adata.deducttype == 2" lay-filter="deducttype">
                </div>
            </div>
            <div class="layui-form-item" v-if="deducttype == 1">
                <label class="layui-form-label">扣杂标准</label>
                <div class="layui-input-block" >
                    <select  type="text" name="isdiscount" lay-filter="isdiscount" lay-verify="required" class="layui-select">
                        <option value="" disabled>请选择</option>
                        <option value="1" :selected="adata.isdiscount == 1"> 按照合同约定的扣杂率扣杂</option>
                        <option value="2" :disabled="paymentmethod == 1" :selected="adata.isdiscount == 2"> 按照质检结果扣杂</option>
                        <option value="3" :disabled="paymentmethod == 1" :selected="adata.isdiscount == 3"> 合同和质检结果取最大扣杂</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item" v-if="deducttype == 2">
                <label class="layui-form-label">扣杂标准</label>
                <div class="layui-input-block">
                    <select   type="text" name="isdiscount" lay-filter="isdiscount" lay-verify="required" class="layui-select">
                        <option value="" disabled>请选择</option>
                        <option value="4" :disabled="paymentmethod == 1" :selected="adata.isdiscount == 4"> 按质检结果分级扣杂</option>
                    </select>
                </div>
            </div>
            <div v-if="isdiscount == 4 && standards.length > 0" style="padding: 10px;background: #f3f3f3;width: 640px;">
                <div class="oitem" v-for="(currStandard, itemNo) in oldStandards" :key="currStandard.id">
                    <div class="rj-oitem-tit">
                        <!--        <span>标准{{itemNo + 1}}</span>-->
                        <!--                    <p id="acts" style="float: right;color: #1aad19;" @click="addItem" v-if="isLast">-->
                        <!--                        <span id="addItem" style="padding-right: 5px;">添加</span>-->
                        <!--                        <span v-if="itemNo != 0">|</span>-->
                        <!--                        <span id="delItem" style="padding-left: 5px;" @click="delItem" v-if="itemNo != 0">删除</span>-->
                        <!--                    </p>-->
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分级标准</label>
                        <div class="layui-input-block">
                            <input type="hidden" :name="'standars[' + itemNo + '].standard'" :value="currStandard.standard">
                            <select class="layui-select standard" :name="'standars[' + itemNo + '].standardid'" :lay-filter="'standardid' + itemNo">
                                <option :value="currStandard" selected>{{currStandard.standard}}</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">单价</label>
                        <div class="layui-input-block">
                            <input class="layui-input price" type="number" :name="'standars[' + itemNo + '].price'" :value="currStandard.price" placeholder="请输入单价">
                        </div>
                    </div>
                </div>

<!--                <EditContractItem v-if="oldStandards.length > 0" v-for="(item, index) in oldStandards" :key="index" :initItemlen="itemlen" :itemNo="index" :itemdata="item" :standards="standards" :isLast="index+1 == oldStandards.length" @doAddItem="addOldItem(index + 1)" @doDelItem="delOldItem"></EditContractItem>-->
<!--                <ContractItem v-if="oldStandards.length <= 0" v-for="(item, index) in oitems" :key="index" :itemNo="item" :isLast="index+1 == oitems.length"-->
<!--                              :standards="standards" @doAddItem="addItem(index + 1)"-->
<!--                              @doDelItem="delItem"></ContractItem>-->
            </div>

            <div class="layui-form-item" style="margin-top: 20px;" v-if="deducttype != 2">
                <label class="layui-form-label">扣杂率(%)</label>
                <div class="layui-input-block">
                    <input type="text" name="discountrate" :value="adata.discountrate" lay-verify="required" ref="discountrate" placeholder="请输入扣杂率" class="layui-input">
                </div>
            </div>
<!--            <div class="layui-form-item" v-if="deducttype == 2">-->
<!--                <label class="layui-form-label">合同约定杂土比率(%)</label>-->
<!--                <div class="layui-input-block">-->
<!--                    <input type="text" name="soilchkrate" :value="adata.soilchkrate" lay-verify="required" placeholder="请输入合同约定杂土比率" class="layui-input">-->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="layui-form-item" v-if="deducttype == 2">-->
<!--                <label class="layui-form-label">合同约定缺陷比率(%)</label>-->
<!--                <div class="layui-input-block">-->
<!--                    <input type="text" name="unqualifiedchkrate" :value="adata.unqualifiedchkrate" lay-verify="required" placeholder="请输入合同约定缺陷比率" class="layui-input">-->
<!--                </div>-->
<!--            </div>-->
            <div class="layui-form-item" style="margin-top: 20px;">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                    <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
                </div>
            </div>

        </form>
    </div>
</template>

<script>
    let qs = require('qs');
    import EditContractItem from "./EditContractItem";
    import ContractItem from "./ContractItem";

    export default {
        name: 'EditContract',
        props: ['id'],
        data () {
            return {
                guid : this.id,
                adata: {},
                batches: [],
                bitchid: '',
                plantors: [],
                partb: '',
                arealands: [],
                arealandid: '',
                products: [],
                product: '',
                specs: [],
                specificationid: '',
                deducttype: null,
                isdiscount: '',
                parta: '',
                companys: [],
                rooms: [],
                room: '',
                departments: [],
                depart: '',
                setlwgtype: 1,
                paymentmethod: 2,
                qatype: 1,
                itemlen: 0,
                oldStandards: '',
                oitems: [0],
                standards: '',
                qamains: '',
            }
        },
        components: {EditContractItem,ContractItem},
        mounted() {
            let that = this;
            this.getAdata();
            this.getPlantors();
            this.getProducts();
            this.getSpecs();
            this.getRooms();
            //this.getDepartment();
            this.getBatches();
            this.getStandards();
            this.getQamains();

            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;

                //提交编辑过的数据
                form.on('submit(submit)', function (data) {
                    data.field.guid = that.guid;
                    let loader = layer.load();
                    $.ajax({
                        url: '/harvest/Procurementcontract!update.action',
                        data: data.field,
                        type: "post",
                        dataType: "json",
                        success: function(res) {
                            if (res.msg == "success") {
                                that.toast("修改成功");
                                that.$parent.switchPage('ContractList');
                            } else {
                                that.toast("系统错误");
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                    return false;
                });

                form.on('select(partb)', function (data) {
                    that.getArealands(data.value);
                })
                // form.on('select(departid)', function (data) {
                //     that.getBatches(data.value);
                // })
                form.on('radio(paymentmethod)', function (data) {
                    that.paymentmethod = data.value;
                    console.log('paymentmethod:', that.paymentmethod)
                })
                form.on('select(isdiscount)', function (data) {
                    that.isdiscount = data.value;
                })
                form.on('radio(qatype)', function (data) {
                    that.qatype = data.value;
                })
                form.on('radio(deducttype)', function (data) {
                    that.deducttype = data.value;
                    if (data.value == 2) {
                        if (that.standards == '') {
                            that.getStandards();
                        }
                        that.isdiscount = 4;
                    } else if (data.value == 1) {
                        that.isdiscount = 1;
                    }
                })
                form.on('select(organizationid)', function (data) {
                    that.getRooms(data.value);
                })
            });
        },
        updated() {
            layui.form.render();
        },
        methods: {
            addOldItem: function(index) {
                this.oldStandards.push(index);
            },
            delOldItem: function() {
                this.oldStandards.pop();
            },
            addItem: function (index) {
                this.oitems.push(index);
            },
            delItem: function () {
                this.oitems.pop();
            },
            //扣杂标准
            getQamains: function () {
                let that = this;
                this.axios({
                    method: 'get',
                    responseType: 'json',
                    url: '/trace/Qamain!getQamain.action',
                    params: {type: 1}
                }).then(response => {
                    if (response.data.msg == "success") {
                        that.qamains = response.data.qamains;
                    } else {
                        this.toastText('获取质检标准数据失败');
                    }
                });
            },
            getStandards: function () {
                let that = this;
                this.axios({
                    method: 'get',
                    responseType: 'json',
                    url: '/harvest/Qastandard!getQastandards.action',
                }).then(response => {
                    if (response.data.msg == "success") {
                        that.standards = response.data.standards;
                    } else {
                        this.toastText('获取扣杂分级标准数据失败');
                    }
                });
            },
            //赋值监听 lsj
            getAdata: function() {
                let loader = layer.load();
                this.axios({
                    mothod: 'post',
                    url: '/harvest/Procurementcontract!getDetail.action',
                    params: {contractid: this.guid}
                }).then(response => {
                    layer.close(loader);
                    if (response.data.msg == 'success') {
                        //获取数据赋值
                        this.deducttype=response.data.contract.deducttype
                        this.adata = response.data.contract;
                        this.oldStandards = response.data.standards || '';
                        this.getArealands(this.adata.partb);
                        this.paymentmethod = this.adata.paymentmethod;
                        this.isdiscount = this.adata.isdiscount;
                        this.qatype = this.adata.qatype;
                    } else {
                        this.toastText('系统错误');
                    }
                })
            },
            loadOrganization: function () {
                this.axios({
                    method: 'post',
                    url: '/sales/Organization!listAll.action'
                }).then((response) => {
                    let res = response.data;
                    if (res.info != undefined && res.info != 'error' ) {
                        this.companys = res.info;
                    } else {
                        that.toast("系统错误");
                    }
                })
            },
            getRooms: function(orgguid) {
                this.axios({
                    method: 'get',
                    url: '/produce/Storeroom!getAllStrm.action',
                    params: {orgguid: orgguid}
                }).then(response => {
                    if (response.data.msg == 'success') {
                        this.rooms = response.data.rooms;
                    } else if (response != undefined && response.data != undefined && response.data.msg == 'sessionTimeOut') {
                        //不不设置提示
                    } else {
                        this.toastText('获取库房数据失败');
                    }
                });
            },
            getPlantors: function() {
                this.axios({
                    method: 'get',
                    url: '/harvest/Procurementcontract!getPlanters.action',
                    params: {}
                }).then(response => {
                    if (response.data.msg == 'success') {
                        this.plantors = response.data.planters;
                    } else {
                        this.toastText('获取种植户数据失败')
                    }
                });
            },
            getBatches: function() {
                this.axios({
                    method: 'get',
                    url: '/harvest/Procurementbatch!findBatckbyTen.action',
                    params: {permission: 38}
                }).then(response => {
                    if (response.data.msg == 'success') {
                        this.batches = response.data.batchs;
                    } else {
                        this.toastText('获取批次数据失败')
                    }
                });
            },
            getArealands: function(plantorid) {
                this.axios({
                    method: 'get',
                    url: '/harvest/Procurementcontract!getArealand.action',
                    params: {userid: plantorid}
                }).then(response => {
                    if (response.data.msg == 'success') {
                        this.arealands = response.data.arealands;
                    } else {
                        this.toastText('获取地块数据失败')
                    }
                });
            },
            getProducts: function() {
                this.axios({
                    method: 'get',
                    url: '/harvest/Procurementcontract!getProductInfo.action',
                    params: {}
                }).then(response => {
                    if (response.data.msg == 'success') {
                        this.products = response.data.products;
                    } else {
                        this.toastText('获取产品数据失败')
                    }
                });
            },
            getSpecs: function() {
                this.axios({
                    method: 'get',
                    url: '/harvest/Procurementcontract!getSpecifications.action',
                    params: {}
                }).then(response => {
                    if (response.data.msg == 'success') {
                        this.specs = response.data.specs;
                    } else {
                        this.toastText('获取产品数据失败')
                    }
                });
            },
            getDepartment: function () {
                let that = this;
                this.axios({
                    method: 'get',
                    responseType: 'json',
                    url: '/sales/Sellform!findDepartment.action',
                    params: {sPermission: 38}
                }).then(response => {
                    if (response.data.status == "true") {
                        that.departments = response.data.result;
                        // this.depart = response.data.result[0];
                    } else if (response.data.status == "false") {
                        this.toastText(response.data.result);
                    }  else {
                        this.toastText('获取部门数据失败');
                    }
                });
            },
        }
    }
</script>
